<?php

include_once("../dao/conexao_class.php");
include_once("../model/email_model_class.php");

class EmailDAO {

    private $insert = "INSERT INTO email(fun_id, ema_nome, ema_tipo) values(?, ?, ?);";
    private $update = "UPDATE email SET ema_nome = ?, ema_tipo = ? WHERE fun_id = ? AND ema_nome = ? AND ema_tipo = ?;";
    private $delete = "DELETE FROM email WHERE fun_id = ? AND ema_nome = ? AND ema_tipo = ?;";
    private $delete_all = "DELETE FROM email WHERE fun_id = ?;";
    private $select = "SELECT * FROM email WHERE fun_id = ?";
    private $select_all = "SELECT * FROM email;";

    function insert($objEmail) {
        //conectando ao banco
        $objConn = new Conexao();
        $conn = $objConn->conecta();
//        var_dump($conn);
        //criando o preparedStatment
        if ($stm = $conn->prepare($this->insert)) {

            $stm->bind_param("iss", $objEmail->getFuncionario(),$objEmail->getNome(),$objEmail->getTipo());

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        } else {
            return false;
        }
    }

    function update($objEmailAntigo, $objEmailNovo) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->update)) {

            $stm->bind_param("ssiss",$objEmailNovo->getNome(),$objEmailNovo->getTipo(),
                    $objEmailAntigo->getFuncionario(),$objEmailAntigo->getNome(),$objEmailAntigo->getTipo());
            
            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        }
        return false;
    }

    function deleteAll($id) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->delete_all)) {

            $stm->bind_param("i", $id);

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        }
        return false;
    }
    
    function delete($objEmail) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->delete)) {

            $stm->bind_param("iss", $objEmail->getFuncionario,$objEmail->getNome(),$objEmail->getTipo());

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        }
        return false;
    }

    function findEmailByFuncionario($idFunc){
        
        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $emaM = new EmailModel();
        $lista = array();
        if($stm = $conn->prepare($this->select)){
            
            $stm->bind_param("i", $idFunc);
            
            $stm->execute();
            
            $stm->bind_result($funcionario,$email,$tipo);
            
            $i = 0;
            while($stm->fetch()){
                
                $emaM = new EmailModel();
                
                $emaM->setFuncionario($funcionario);
                $emaM->setNome($email);
                $emaM->setTipo($tipo);
                
                $lista[$i++] = $emaM;
            }
            
            $conn->close();
            return $lista;
        }
    }
    
    function findAllEmail() {

        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $emaM = new EmailModel();
        $lista = array();
        if ($stm = $conn->prepare($this->select_all)) {

            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($funcionario,$email,$tipo);


            //para cada resultado encontrado
            $i = 0;
            while($stm->fetch()){
                $emaM = new EmailModel();
                
                $emaM->setFuncionario($funcionario);
                $emaM->setNome($email);
                $emaM->setTipo($tipo);
                
                $lista[$i++] = $emaM;
            }

            $conn->close();
            return $lista;
        }
    }

}

//$teste = new EmailDAO();
//$ema = new EmailModel();
////$ema->setId(3);
//$ema->setDescricao("Administraçao");
//
////$ema = 3;
//if($teste->insert($ema)){
//    printf("Deletado com sucesso!");
//}else{
//    printf("Erro ao deletar!");
//}
//
//var_dump($teste->findAllEmail());

